<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>GSoC2011SfM: D:/Travail/These/Determination caracteristiques camera/GSoC/SfM/src/Visualizer.cpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  <td id="projectlogo"><img alt="Logo" src="logo.png"/></td>
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">GSoC2011SfM
   &#160;<span id="projectnumber">0.1</span>
   </div>
   <div id="projectbrief">Google Summer of Code 2011: Structure from motion</div>
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">D:/Travail/These/Determination caracteristiques camera/GSoC/SfM/src/Visualizer.cpp</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="preprocessor">#include &lt;pcl/io/vtk_io.h&gt;</span>
<a name="l00003"></a>00003 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="preprocessor">#include &quot;Visualizer.h&quot;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &quot;PCL_mapping.h&quot;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &quot;PointOfView.h&quot;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &quot;Camera.h&quot;</span>
<a name="l00009"></a>00009 
<a name="l00010"></a>00010 <span class="keyword">using</span> cv::Vec3d;
<a name="l00011"></a>00011 <span class="keyword">using</span> cv::Mat;
<a name="l00012"></a>00012 
<a name="l00013"></a>00013 <span class="keyword">namespace </span>OpencvSfM{
<a name="l00014"></a><a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a493c0f542bd636db216145734a5d3ee5">00014</a>   <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a493c0f542bd636db216145734a5d3ee5">Visualizer::Visualizer</a>( std::string name )
<a name="l00015"></a>00015     :viewer( new pcl::visualization::PCLVisualizer ( name ) )
<a name="l00016"></a>00016   {
<a name="l00017"></a>00017     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;setBackgroundColor ( 0, 0, 0 );
<a name="l00018"></a>00018     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;addCoordinateSystem ( 0.1 );
<a name="l00019"></a>00019     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;initCameraParameters ( );
<a name="l00020"></a>00020   }
<a name="l00021"></a>00021 
<a name="l00022"></a><a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a304a33dc1adcbc061c65b8eec87c6fcc">00022</a>   <span class="keywordtype">void</span> <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a304a33dc1adcbc061c65b8eec87c6fcc">Visualizer::addCamera</a>( <span class="keyword">const</span> <a class="code" href="class_opencv_sf_m_1_1_point_of_view.html" title="This class represent the 3D position of the device which take the pictures. The role of the class is ...">PointOfView</a>&amp; camera,
<a name="l00023"></a>00023     std::string name, <span class="keywordtype">int</span> viewport )
<a name="l00024"></a>00024   {
<a name="l00025"></a>00025     <span class="comment">//First get the focal of camera:</span>
<a name="l00026"></a>00026     <span class="keyword">const</span> cv::Ptr&lt;Camera&gt; cam = camera.<a class="code" href="class_opencv_sf_m_1_1_point_of_view.html#ae5de2e16eac553ea1e1e7501e009bbbd">getIntraParameters</a>( );
<a name="l00027"></a>00027     <span class="keywordtype">double</span> focal = cam-&gt;getFocal( );
<a name="l00028"></a>00028     <span class="comment">//Now get the corners in camera&#39;s coordinates:</span>
<a name="l00029"></a>00029     std::vector&lt;cv::Vec2d&gt; corners, real_coord;
<a name="l00030"></a>00030     corners.push_back( cv::Vec2d( 0, 0 ) );
<a name="l00031"></a>00031     corners.push_back( cv::Vec2d( 640, 480 ) );
<a name="l00032"></a>00032 
<a name="l00033"></a>00033     real_coord = cam-&gt;pixelToNormImageCoordinates( corners );
<a name="l00034"></a>00034 
<a name="l00035"></a>00035     <span class="comment">//create the mesh:</span>
<a name="l00036"></a>00036     std::vector&lt;Vec3d&gt; camera_mesh;
<a name="l00037"></a>00037     camera_mesh.push_back( Vec3d(
<a name="l00038"></a>00038       real_coord[ 0 ][ 0 ], real_coord[ 0 ][ 1 ], 0 ) );<span class="comment">//bottom left corner</span>
<a name="l00039"></a>00039     camera_mesh.push_back( Vec3d(
<a name="l00040"></a>00040       real_coord[ 1 ][ 0 ], real_coord[ 0 ][ 1 ], 0 ) );<span class="comment">//bottom right corner</span>
<a name="l00041"></a>00041     camera_mesh.push_back( Vec3d(
<a name="l00042"></a>00042       real_coord[ 1 ][ 0 ], real_coord[ 1 ][ 1 ], 0 ) );<span class="comment">//top right corner</span>
<a name="l00043"></a>00043     camera_mesh.push_back( Vec3d(
<a name="l00044"></a>00044       real_coord[ 0 ][ 0 ], real_coord[ 1 ][ 1 ], 0 ) );<span class="comment">//top left corner</span>
<a name="l00045"></a>00045     camera_mesh.push_back( Vec3d(
<a name="l00046"></a>00046       0, 0, focal ) );<span class="comment">//camera&#39;s origin</span>
<a name="l00047"></a>00047 
<a name="l00048"></a>00048     <span class="comment">//move theses points to the world coordinates:</span>
<a name="l00049"></a>00049     <span class="keywordflow">for</span>( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; camera_mesh.size( ); ++i )
<a name="l00050"></a>00050     {
<a name="l00051"></a>00051       <span class="comment">//translate to the correct location:</span>
<a name="l00052"></a>00052       camera_mesh[ i ] = ( Vec3d ) ( Mat ) ( ( Mat )( camera_mesh[ i ] ) -
<a name="l00053"></a>00053         camera.<a class="code" href="class_opencv_sf_m_1_1_point_of_view.html#a79b41aa3dc0ff0f7f5c0d821a8688f1f">getTranslationVector</a>( ) );
<a name="l00054"></a>00054       <span class="comment">//rotate the ith point:</span>
<a name="l00055"></a>00055       camera_mesh[ i ] = ( Vec3d ) ( Mat ) ( ( Mat )( camera_mesh[ i ] ).t( ) *
<a name="l00056"></a>00056         camera.<a class="code" href="class_opencv_sf_m_1_1_point_of_view.html#af13aa49ebfdb95b056b20825b00d3866">getRotationMatrix</a>( ) );
<a name="l00057"></a>00057     }
<a name="l00058"></a>00058     pcl::PointCloud&lt;pcl::PointXYZ&gt;::Ptr camera_cloud (
<a name="l00059"></a>00059       <span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZ&gt; );
<a name="l00060"></a>00060     mapping::convert_OpenCV_vector( camera_mesh, *camera_cloud );
<a name="l00061"></a>00061 
<a name="l00062"></a>00062     <span class="comment">//now create the mesh himself:</span>
<a name="l00063"></a>00063     std::vector&lt;pcl::Vertices&gt; vertices;
<a name="l00064"></a>00064     pcl::Vertices vertice;
<a name="l00065"></a>00065     vertice.vertices.push_back( 0 );
<a name="l00066"></a>00066     vertice.vertices.push_back( 1 );
<a name="l00067"></a>00067     vertice.vertices.push_back( 2 );
<a name="l00068"></a>00068     vertice.vertices.push_back( 3 );
<a name="l00069"></a>00069     vertices.push_back( vertice ); vertice.vertices.clear( );
<a name="l00070"></a>00070     vertice.vertices.push_back( 0 );
<a name="l00071"></a>00071     vertice.vertices.push_back( 1 );
<a name="l00072"></a>00072     vertice.vertices.push_back( 4 );
<a name="l00073"></a>00073     vertices.push_back( vertice ); vertice.vertices.clear( );
<a name="l00074"></a>00074     vertice.vertices.push_back( 1 );
<a name="l00075"></a>00075     vertice.vertices.push_back( 2 );
<a name="l00076"></a>00076     vertice.vertices.push_back( 4 );
<a name="l00077"></a>00077     vertices.push_back( vertice ); vertice.vertices.clear( );
<a name="l00078"></a>00078     vertice.vertices.push_back( 2 );
<a name="l00079"></a>00079     vertice.vertices.push_back( 3 );
<a name="l00080"></a>00080     vertice.vertices.push_back( 4 );
<a name="l00081"></a>00081     vertices.push_back( vertice ); vertice.vertices.clear( );
<a name="l00082"></a>00082     vertice.vertices.push_back( 3 );
<a name="l00083"></a>00083     vertice.vertices.push_back( 0 );
<a name="l00084"></a>00084     vertice.vertices.push_back( 4 );
<a name="l00085"></a>00085     vertices.push_back( vertice ); vertice.vertices.clear( );
<a name="l00086"></a>00086 
<a name="l00087"></a>00087     pcl::PolygonMesh triangles;
<a name="l00088"></a>00088     sensor_msgs::PointCloud2 msg;
<a name="l00089"></a>00089     pcl::toROSMsg( *camera_cloud, msg );
<a name="l00090"></a>00090     triangles.cloud = msg;
<a name="l00091"></a>00091     triangles.polygons = vertices;
<a name="l00092"></a>00092 
<a name="l00093"></a>00093     pcl::io::saveVTKFile ( <span class="stringliteral">&quot;meshCam.vtk&quot;</span>, triangles );
<a name="l00094"></a>00094     <span class="comment">//now add mesh to the viewer:</span>
<a name="l00095"></a>00095     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;addPolygonMesh( triangles, name, viewport );
<a name="l00096"></a>00096     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;setShapeRenderingProperties(
<a name="l00097"></a>00097       pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 1.5, name );
<a name="l00098"></a>00098 
<a name="l00099"></a>00099     <span class="comment">//viewer-&gt;addPointCloud&lt;pcl::PointXYZ&gt;( camera_cloud, name );</span>
<a name="l00100"></a>00100     <span class="comment">//viewer-&gt;setPointCloudRenderingProperties (</span>
<a name="l00101"></a>00101     <span class="comment">//  pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, name );</span>
<a name="l00102"></a>00102   }
<a name="l00103"></a><a class="code" href="class_opencv_sf_m_1_1_visualizer.html#ab196cccf4e3762e2728ea4edc52180fb">00103</a>   <span class="keywordtype">void</span> <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#ab196cccf4e3762e2728ea4edc52180fb">Visualizer::add3DPoints</a>( <span class="keyword">const</span> std::vector&lt;cv::Vec3d&gt;&amp; points,
<a name="l00104"></a>00104     std::string name, <span class="keywordtype">int</span> viewport )
<a name="l00105"></a>00105   {
<a name="l00106"></a>00106     <span class="keywordflow">if</span>(points.size() == 0)
<a name="l00107"></a>00107       <span class="keywordflow">return</span>;
<a name="l00108"></a>00108     pcl::PointCloud&lt; pcl::PointXYZ &gt;::Ptr my_cloud (
<a name="l00109"></a>00109       <span class="keyword">new</span> pcl::PointCloud&lt; pcl::PointXYZ &gt; );
<a name="l00110"></a>00110     mapping::convert_OpenCV_vector( points, *my_cloud );
<a name="l00111"></a>00111 
<a name="l00112"></a>00112     pcl::visualization::PointCloudColorHandlerCustom&lt;pcl::PointXYZ&gt;
<a name="l00113"></a>00113       single_color( my_cloud, 0, 255, 0 );
<a name="l00114"></a>00114     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;addPointCloud&lt;pcl::PointXYZ&gt; ( my_cloud, single_color,
<a name="l00115"></a>00115       name, viewport );
<a name="l00116"></a>00116     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;setPointCloudRenderingProperties (
<a name="l00117"></a>00117       pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, name, viewport );
<a name="l00118"></a>00118 
<a name="l00119"></a>00119     pcl::PolygonMesh triangles;
<a name="l00120"></a>00120     sensor_msgs::PointCloud2 msg;
<a name="l00121"></a>00121     pcl::toROSMsg( *my_cloud, msg );
<a name="l00122"></a>00122     triangles.cloud = msg;
<a name="l00123"></a>00123     pcl::io::saveVTKFile ( ((std::string)<span class="stringliteral">&quot;test&quot;</span>)+name+((std::string)<span class="stringliteral">&quot;.vtk&quot;</span>),
<a name="l00124"></a>00124       triangles );
<a name="l00125"></a>00125 
<a name="l00126"></a>00126   }
<a name="l00127"></a><a class="code" href="class_opencv_sf_m_1_1_visualizer.html#aae0430188f5f68dd792d2a2d8305d690">00127</a>   <span class="keywordtype">void</span> <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#aae0430188f5f68dd792d2a2d8305d690">Visualizer::add3DPointsColored</a>( <span class="keyword">const</span> std::vector&lt;cv::Vec3d&gt;&amp; points,
<a name="l00128"></a>00128      <span class="keyword">const</span> std::vector&lt;unsigned int&gt;&amp; colors, std::string name, <span class="keywordtype">int</span> viewport )
<a name="l00129"></a>00129   {
<a name="l00130"></a>00130     <span class="keywordflow">if</span>(points.size() == 0)
<a name="l00131"></a>00131       <span class="keywordflow">return</span>;
<a name="l00132"></a>00132 
<a name="l00133"></a>00133     <span class="comment">// --------------------------------------------</span>
<a name="l00134"></a>00134     <span class="comment">// -----Open 3D viewer and add point cloud-----</span>
<a name="l00135"></a>00135     <span class="comment">// --------------------------------------------</span>
<a name="l00136"></a>00136 
<a name="l00137"></a>00137 
<a name="l00138"></a>00138     pcl::PointCloud&lt; pcl::PointXYZRGB &gt;::Ptr my_cloud (
<a name="l00139"></a>00139       <span class="keyword">new</span> pcl::PointCloud&lt; pcl::PointXYZRGB &gt; );
<a name="l00140"></a>00140     <span class="comment">//process point per point (can&#39;t convert directly...)</span>
<a name="l00141"></a>00141     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> max_size = points.size();
<a name="l00142"></a>00142     <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i&lt;max_size; i++)
<a name="l00143"></a>00143     {
<a name="l00144"></a>00144       pcl::PointXYZRGB p;
<a name="l00145"></a>00145       p.data[0] = points[i].val[0];
<a name="l00146"></a>00146       p.data[1] = points[i].val[1];
<a name="l00147"></a>00147       p.data[2] = points[i].val[2];
<a name="l00148"></a>00148       p.rgb = *((<span class="keywordtype">float</span>*)&amp;(colors[i]));
<a name="l00149"></a>00149       my_cloud-&gt;push_back(p);
<a name="l00150"></a>00150     }
<a name="l00151"></a>00151     pcl::visualization::PointCloudColorHandlerRGBField&lt;pcl::PointXYZRGB&gt; rgb(my_cloud);
<a name="l00152"></a>00152     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;addPointCloud&lt;pcl::PointXYZRGB&gt; (my_cloud, rgb, name, viewport );
<a name="l00153"></a>00153     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;setPointCloudRenderingProperties (
<a name="l00154"></a>00154       pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, name, viewport );
<a name="l00155"></a>00155 
<a name="l00156"></a>00156     pcl::PolygonMesh triangles;
<a name="l00157"></a>00157     sensor_msgs::PointCloud2 msg;
<a name="l00158"></a>00158     pcl::toROSMsg( *my_cloud, msg );
<a name="l00159"></a>00159     triangles.cloud = msg;
<a name="l00160"></a>00160     
<a name="l00161"></a>00161     <span class="comment">//pcl::io::saveVTKFile ( ((std::string)&quot;test&quot;)+name+((std::string)&quot;.vtk&quot;), triangles );</span>
<a name="l00162"></a>00162 
<a name="l00163"></a>00163   }
<a name="l00164"></a>00164 
<a name="l00165"></a><a class="code" href="class_opencv_sf_m_1_1_visualizer.html#af39fe6832962888e4c81c5a4ae6c6f55">00165</a>   <span class="keywordtype">void</span> <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#af39fe6832962888e4c81c5a4ae6c6f55">Visualizer::runInteract</a>( )
<a name="l00166"></a>00166   {
<a name="l00167"></a>00167     <a class="code" href="class_opencv_sf_m_1_1_visualizer.html#a7bd6c0081e57cd9d25b2d495a3afa2db" title="The PCL viewer.">viewer</a>-&gt;spin( );
<a name="l00168"></a>00168   }
<a name="l00169"></a>00169 }
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Sun Aug 21 2011 16:45:52 for GSoC2011SfM by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5.1
</small></address>

</body>
</html>
